package com.lphl.pet.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lphl.pet.GloableParams;
import com.lphl.pet.MyApplication;
import com.lphl.pet.R;
import com.lphl.pet.util.EditImage;

import java.io.File;

/**
 * Created by Administrator on 2015/7/24.
 */
public class ShowPicActivity extends Activity implements View.OnClickListener  {

    private ImageView ivBack;
    private ImageView ivShowImg;
    private TextView tvImgTitle;
    private LinearLayout llShowPicBottomShare;
    private LinearLayout llShowPicBottomLeft;
    private LinearLayout llShowPicBottomRight;
    private LinearLayout llShowPicBottomRename;
    private LinearLayout llShowPicBottomDelete;

    private EditImage mEditImage;
    /**
     * 临时保存
     */
    private Bitmap mTmpBmp;
    private Bitmap mBitmap;
    private String imgUrl;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_show_pic);

        initView();
        initData();
    }



    private void initView() {
        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivShowImg = (ImageView) findViewById(R.id.iv_show_img);
        tvImgTitle = (TextView)findViewById(R.id.tv_img_title);
        llShowPicBottomShare = (LinearLayout) findViewById(R.id.ll_show_pic_bottom_share);
        llShowPicBottomLeft = (LinearLayout) findViewById(R.id.ll_show_pic_bottom_left);
        llShowPicBottomRight = (LinearLayout) findViewById(R.id.ll_show_pic_bottom_right);
        llShowPicBottomRename = (LinearLayout) findViewById(R.id.ll_show_pic_bottom_rename);
        llShowPicBottomDelete = (LinearLayout) findViewById(R.id.ll_show_pic_bottom_delete);


        ivBack.setOnClickListener(this);
        llShowPicBottomLeft.setOnClickListener(this);
        llShowPicBottomRight.setOnClickListener(this);
        llShowPicBottomRename.setOnClickListener(this);
        llShowPicBottomDelete.setOnClickListener(this);
    }


    private void initData() {
        imgUrl = getIntent().getStringExtra("imgUrl");
        path = GloableParams.SDCardPath +imgUrl;
        tvImgTitle.setText(imgUrl.substring(0,imgUrl.lastIndexOf(".")));
        mEditImage = new EditImage(ShowPicActivity.this);
        MyApplication.initImageLoader(ShowPicActivity.this).displayImage(path, ivShowImg);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.ll_show_pic_bottom_share:

                break;
            case R.id.ll_show_pic_bottom_left:
                rotate(-90);
                break;
            case R.id.ll_show_pic_bottom_right:
                rotate(90);
                break;
            case R.id.ll_show_pic_bottom_rename:
                path = GloableParams.SDCardImagePath + imgUrl;
                showUIRenameDialog(path);
                break;
            case R.id.ll_show_pic_bottom_delete:
                path = GloableParams.SDCardImagePath + imgUrl;
                deleteImg(path);
                intent = new Intent(ShowPicActivity.this, PicListActivity.class);
                intent.putExtra("isRemove", GloableParams.isRemove);
                startActivity(intent);
                this.finish();
                break;
        }
    }

    /**
     * 删除图片
     * @param path
     */
    private void deleteImg(String path) {
        File imgFile = new File(path);
        imgFile.delete();
    }

    /**
     * 重命名窗口
     */
    private void showUIRenameDialog(final String path) {
        final Dialog dialog = new Dialog(ShowPicActivity.this, R.style.Dialog);
        dialog.setContentView(R.layout.dialog_rename);
        dialog.setCancelable(false);
        dialog.getWindow().setLayout(dialog.getWindow().getWindowManager().getDefaultDisplay().getWidth(),
                dialog.getWindow().getAttributes().WRAP_CONTENT);
        final EditText etRename = (EditText) dialog.findViewById(R.id.et_rename);
        TextView tvCancel = (TextView) dialog.findViewById(R.id.tv_cancel);
        TextView tvOk = (TextView) dialog.findViewById(R.id.tv_ok);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String reName = etRename.getText().toString();
                if(!TextUtils.isEmpty(reName)){
                    tvImgTitle.setText(reName);
                    renameImage(reName, path);
                    dialog.dismiss();

                    Intent intent = new Intent(ShowPicActivity.this, PicListActivity.class);
                    intent.putExtra("isRename", GloableParams.isRename);
                    startActivity(intent);
                }
            }
        });
        dialog.show();
    }

    /**
     * 图片重命名
     * @param path
     */
    private void renameImage(String title, String path) {
        File imgFile = new File(GloableParams.SDCardImagePath+imgUrl);
        File saveRename = new File(GloableParams.SDCardImagePath + title+ imgUrl.substring(imgUrl.lastIndexOf("."),imgUrl.length()));
        imgFile.renameTo(saveRename);
    }

    /**
     * 旋转角度
     * @param i  -90左旋转   90 右旋转
     */
    private void rotate(float i) {
        String path = GloableParams.SDCardImagePath + imgUrl;
        zoomLoadImg(path);

        mTmpBmp = mEditImage.rotate(mBitmap, i);
        reset();
    }

    /**
     * 缩放加载图片
     * @param path
     */
    private void zoomLoadImg(String path) {
        // 得到图片的宽和高
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true; // 加载器不加载图片, 而是把图片的out(宽和高)的字段信息取出来
        BitmapFactory.decodeFile(path, opts);
        int imageWidth = opts.outWidth;
        int imageHeight = opts.outHeight;

        // 得到屏幕的宽和高
        Display display = getWindowManager().getDefaultDisplay();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();

        // 计算缩放比例
        int widthScale = imageWidth / screenWidth;
        int heightScale = imageHeight / screenHeight;

        int scale = widthScale > heightScale ? widthScale : heightScale;

        // 使用缩放比例进行缩放加载图片
        opts.inJustDecodeBounds = false; // 加载器就会返回图片了
        opts.inSampleSize = scale;
        mBitmap = BitmapFactory.decodeFile(path,opts);
    }

    /**
     * 重新设置一下图片
     */
    private void reset() {
        ivShowImg.setImageBitmap(mTmpBmp);
        ivShowImg.invalidate();
    }
}
